package com.bjfu.mapper;

import com.bjfu.model.Achievement;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AchievementMapper {

    // 插入一个新的科研成果
    @Insert("INSERT INTO achievement (title, category, summary, description, content, attachment_link,created_time,status, team_id) VALUES (#{title}, #{category}, #{summary}, #{description}, #{content}, #{attachmentLink}, #{createdTime},#{status},#{teamId})")
    @Options(useGeneratedKeys = true, keyProperty = "achievementId", keyColumn = "achievement_id")
    int insertAchievement(Achievement achievement);

    // 更新科研成果信息 新增：status=2，3 未公开不可编辑
    @Update("UPDATE achievement SET title = #{title}, category = #{category}, summary = #{summary}, description = #{description}, " +
            "content = #{content}, attachment_link = #{attachmentLink}, created_time = #{createdTime}, status = #{status}, team_id = #{teamId} " +
            "WHERE achievement_id = #{achievementId}  AND (status =2 OR status=3)")
    int updateAchievement(Achievement achievement);


    // 删除科研成果
    @Delete("DELETE FROM achievement WHERE achievement_id = #{achievementId}")
    int deleteAchievement(Long achievementId);

    // 根据 achievement_id 获取成果信息
    @Select("SELECT * FROM achievement WHERE achievement_id = #{achievement_id}")
    Achievement getAchievementById(Long achievement_id);

    // 获取所有科研成果
    @Select("SELECT * FROM achievement")
    List<Achievement> getAllAchievements();

    // 根据团队 ID 获取该团队的所有科研成果
    @Select("SELECT * FROM achievement WHERE team_id = #{teamId}")
    List<Achievement> getAchievementsByTeamId(Long teamId);

    @Update("UPDATE achievement SET status = #{pass_status} WHERE achievement_id = #{achievement_id}")
    int judgeAchievement(Long achievement_id, Integer pass_status);

    // 查询所有待审核的科研成果
    @Select("SELECT * FROM achievement WHERE status = 1")  // 1是待审核的状态
    List<Achievement> getAchievementsNotJudged();

    @Select("SELECT * FROM achievement WHERE status = 2")  // 2是已发布的状态
    List<Achievement> getOpenAchievements();

    @Select("SELECT * FROM achievement WHERE status = 2 AND category = #{category}")
    List<Achievement> getAchievementsByCategory(Integer category);

    @Select("SELECT * FROM achievement ")  // 所有成果
     List<Achievement> getAchievements();

    @Select("SELECT * FROM achievement WHERE category = #{category}")
    List<Achievement>  getAchievement(Integer category);

    @Select("SELECT * FROM achievement WHERE status = 4")  // 是不公开的状态
    List<Achievement>getNotOpenAchievements();

    @Select("SELECT * FROM achievement WHERE status = 4 AND category = #{category}")//获取某类不公开成果
    List<Achievement> getNotOpenAchievement(Integer category);

    @Update("UPDATE achievement SET status = #{status} WHERE achievement_id = #{achievementId}")
    int changeAchievementStatus(Achievement achievement);
}